package dao;

import utils.connect.ConnectSqlDaoImpl;
import utils.string.sql.T_Equipment;
import utils.string.sql.T_Fix;
import vo.Fix;

import java.sql.*;
import java.util.HashMap;

public class FixDaoImpl implements FixDao {
    ConnectSqlDaoImpl connectSqlDao = ConnectSqlDaoImpl.getInStance();
    @Override
    public boolean deleteByID(int id) {
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "delete from "+ T_Fix.TABLE_NAME+" where " +
                T_Fix.FIX_ID + " = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public HashMap selectAll() {
        HashMap fixs = new HashMap();
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "select * from "+ T_Fix.TABLE_NAME;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                Fix fix = new Fix();
                fix.setEquip_id(resultSet.getInt(T_Fix.EQUIP_ID));
                fix.setFix_date(resultSet.getDate(T_Fix.FIX_DATE));
                fix.setUser_id(resultSet.getInt(T_Fix.USER_ID));
                fix.setFix_reason(resultSet.getString(T_Fix.FIX_REASON));
                fix.setFix_num(resultSet.getInt(T_Fix.FIX_NUM));
                fix.setFix_id(resultSet.getInt(T_Fix.FIX_ID));
                fixs.put(fix.getFix_id(),fix);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return fixs;
    }

    @Override
    public boolean updateById(Fix fix, int id) {
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "update "+ T_Fix.TABLE_NAME+" set "
                +T_Fix.USER_ID+"= ? "
                +T_Fix.EQUIP_ID + " = ?"
                +T_Fix.FIX_DATE + " = ?"
                +T_Fix.FIX_REASON + " = ?"
                +T_Fix.FIX_NUM + " = ?"
                + " where "+ T_Fix.FIX_ID +" = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,fix.getUser_id());
            preparedStatement.setInt(2,fix.getEquip_id());
            preparedStatement.setDate(3, (Date) fix.getFix_date());
            preparedStatement.setString(4,fix.getFix_reason());
            preparedStatement.setInt(5,fix.getFix_num());
            preparedStatement.setInt(6,fix.getFix_id());
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean insertFix(Fix fix) {
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "insert into "+ T_Fix.TABLE_NAME+"("
                +T_Fix.USER_ID+","
                +T_Fix.EQUIP_ID + ","
                +T_Fix.FIX_DATE + " ,"
                +T_Fix.FIX_REASON + ","
                +T_Fix.FIX_NUM + " )"
                + " values (?,?,?,?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,fix.getUser_id());
            preparedStatement.setInt(2,fix.getEquip_id());
            preparedStatement.setDate(3, (Date) fix.getFix_date());
            preparedStatement.setString(4,fix.getFix_reason());
            preparedStatement.setInt(5,fix.getFix_num());
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
